package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppBiaoqian;
import com.vf.admin.miaodong.service.AfterBiaoqianService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个性标签管理
 */
@Controller
@RequestMapping("/after/biaoqian")
public class AfterBiaoqianController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AfterBiaoqianService afterBiaoqianService;


    /**
     * 查询list
     * @param leixing 标签类型  1个性标签   2兴趣爱好
     * @param name 标签名
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean, Integer leixing,String name) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(leixing)) {
                paramap.put("leixing", leixing);
                request.setAttribute("leixing", leixing);
            }
            if (StringEx.isNotNull(name)) {
                paramap.put("name", name);
                request.setAttribute("name", name);
            }
            List<AppBiaoqian> list = afterBiaoqianService.list(paramap, splitPageBean);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/biaoqian/list");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }


    /**
     * @Description 添加个性标签
     * @Param: [appBanner]
     * @return: com.vf.admin.common.entity.Result
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppBiaoqian appBiaoqian) {
        try {
            if (StringUtils.isEmpty(appBiaoqian.getName())) {
                return errorJSON("标签名不能为空");
            }
            if (StringUtils.isEmpty(appBiaoqian.getLeixing())) {
                return errorJSON("类型不能为空");
            }
            System.out.println("state:"+appBiaoqian.getState());
            if (appBiaoqian.getId() != null) {
                AppBiaoqian biaoqian = afterBiaoqianService.get(appBiaoqian.getId());
                biaoqian.setName(appBiaoqian.getName());
                biaoqian.setLeixing(appBiaoqian.getLeixing());
                biaoqian.setState(appBiaoqian.getState()==null?0:1);
                afterBiaoqianService.save(biaoqian);
            } else {
                appBiaoqian.setCreatetime(new Date());
                afterBiaoqianService.save(appBiaoqian);
            }
            return sucessJSON("成功");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }
    /**
     * 详情
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppBiaoqian appBiaoqian = afterBiaoqianService.get(id);
            request.setAttribute("appBiaoqian", appBiaoqian);
            return this.sucess("/web/after/biaoqian/edit");
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return this.error("系统繁忙");
        }
    }

}
